package com.fjzx.androidframe.global.entity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * 带状态的LiveData数据
 *
 * @param <T>
 * @author xiongyongcong
 */
public class Resource<T> {
    @NonNull
    public String action;
    @NonNull
    public UiStatus status;
    @Nullable
    public T data;
    @Nullable
    public String errorMessage;
    public int loadingMsgId;

    public Resource(String action) {
        this.action = action;
    }

    public void success(@NonNull T data) {
        this.status = UiStatus.SUCCESS;
        this.data = data;
    }

    public void error(String msg) {
        this.status = UiStatus.ERROR;
        this.errorMessage = msg;
    }

    public void loading(int msgId) {
        this.status = UiStatus.LOADING;
        this.loadingMsgId = msgId;
    }

    public void loading() {
        this.status = UiStatus.LOADING;
    }

    @Override
    public String toString() {
        return "Resource{" +
                "action='" + action + '\'' +
                ", status=" + status +
                ", data=" + data +
                ", errorMessage='" + errorMessage + '\'' +
                ", loadingMsgId=" + loadingMsgId +
                '}';
    }
}